package com.ly.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.SepClassDto;
import com.ly.cloud.dto.SeparateClassRuleDto;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.service.SeparateClassRuleService;
import com.ly.cloud.service.TemporaryStudentService;
import com.ly.cloud.vo.ClassStuVo;
import com.ly.cloud.vo.SeparateClassRuleVo;
import com.ly.cloud.web.utils.WebResponse;
import io.swagger.annotations.Api;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Description: 临时学生Controller
 * @Author: yixuan
 * @Date: 2018/12/20
 */

@RestController
@RequestMapping("/tempStu")
@Api("临时学生Controller")
public class TemporaryStudentController {


    @Autowired
    private TemporaryStudentService temporaryStudentService;

    @Autowired
    private SeparateClassRuleService separateClassRuleService ;

    /**
     * @Description: 分页+条件 查询 原型图左边
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/queryPageListLeft", method = RequestMethod.GET)
    public WebResponse<PageInfo<Map<String, String>>> queryPageListLeft(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
                                                                        @RequestParam(required = false,value = "grade")String grade,
                                                                        @RequestParam(required = false,value = "profe")String profe,
                                                                        @RequestParam(required = false,value = "name")String name) {
        Page<Map<String, String>> page = temporaryStudentService.selectPageLeft(new Page<Map<String, String>>(pageNum, pageSize), grade,profe,name);
        return new WebResponse<PageInfo<Map<String, String>>>().success(PageInfo.toPageInfo(page));
    }


    /**
     * 根据id删除
     * @param xsid
     * @return
     */
    @RequestMapping(value = "/deleteById/{xsid}", method = RequestMethod.POST)
    public WebResponse<Integer> deleteById(@PathVariable("xsid") String xsid){
        int result = temporaryStudentService.deleteById(xsid) ;
        return new WebResponse<Integer>().success(result);
    }

    /**
     * 根据id批量删除
     * @param xsids
     * @return
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public WebResponse<Integer> deleteByIds(@RequestBody List<String> xsids){
        int result = temporaryStudentService.deleteByIds(xsids) ;
        return new WebResponse<Integer>().success(result);
    }



    /**
     * @Description: 导入
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    public WebResponse<Boolean> uploadExcel(@RequestParam("file") MultipartFile file){
        temporaryStudentService.importExcel(file);
        return new WebResponse<Boolean>().success(true);
    }


    /**
     * @Description: 下载模板
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
    public void downloadExcel(HttpServletResponse response){
        InputStream in = null ;
        OutputStream out = null ;
        try {
            //下载的文件名
            String fileName = "招生数数据-导入模板.xls";
            //读取文件
            ClassPathResource resource = new ClassPathResource("template/招生数数据-导入模板.xls");
            // 设置强制下载不打开
            response.setContentType("application/force-download");
            // 设置文件名
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            in = resource.getInputStream();
            out = response.getOutputStream();
            IOUtils.copy(in, out);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("文件下载失败!", e);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        //return new WebResponse<Boolean>().success(true);
    }

    // ------------------------------------------------------------------------------

    /**
     * @Description: 分页+条件 查询 原型图右边
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/queryPageListRight", method = RequestMethod.GET)
    public WebResponse<PageInfo<Map<String, String>>> queryPageListRight(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
                                                                         @RequestParam(required = false,value = "grade")String grade,
                                                                         @RequestParam(required = false,value = "profe")String profe,
                                                                         @RequestParam(required = false,value = "college")String college) {
        Page<Map<String, String>> page = temporaryStudentService.selectPageRight(new Page<Map<String, String>>(pageNum, pageSize), grade,profe,college);
        return new WebResponse<PageInfo<Map<String, String>>>().success(PageInfo.toPageInfo(page));
    }


    /**
    * @Description: 点击分班弹窗显示分班规则信息
    * @Author: pengshiqing
    * @CreateDate: 2018/12/24
    */
    @RequestMapping(value = "/showSeparateClassPop", method = RequestMethod.GET)
    public WebResponse<List<SeparateClassRuleVo>> showSeparateClassPop(){
        List<SeparateClassRuleVo> list = separateClassRuleService.findAll();
        return new WebResponse<List<SeparateClassRuleVo>>().success(list);
    }


    /**
     * @Description: 保存分班规则
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/updateSeparateClassRule", method = RequestMethod.POST)
    public WebResponse<Boolean> updateSeparateClassRule(@RequestBody List<SeparateClassRuleDto> list){
        separateClassRuleService.update(list);
        return new WebResponse<Boolean>().success(true);
    }


    /**
     * @Description: 编学号
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/editStudentId", method = RequestMethod.POST)
    public WebResponse<Integer> editStudentId(@RequestParam("type") String type , @RequestBody List<String> list){
        //type =1:根据姓名排序，tpye=2,根据考生号排序
        int result = temporaryStudentService.editStudentId(type ,list);
        return new WebResponse<Integer>().success(result);
    }

    /**
     * @Description: 转入正式库
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/intoFormalLibrary", method = RequestMethod.POST)
    public WebResponse<Integer> intoFormalLibrary(@RequestBody List<String> list){
        int result = temporaryStudentService.intoFormalLibrary(list);
        return new WebResponse<Integer>().success(result);
    }


    /**
     * @Description: 根据班级代码查询该班级所有的学生分页
     * @Author: pengshiqing
     * @CreateDate: 2018/12/25
     */
    @RequestMapping(value = "/findAllStuByClassId", method = RequestMethod.GET)
    public WebResponse<PageInfo<ClassStuVo>> findAllStuByClassId(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
                                                                 @RequestParam("classId")String classId,
                                                                 @RequestParam(required = false,value = "ksh")String ksh,
                                                                 @RequestParam(required = false,value = "name")String name) {
        Page<ClassStuVo> page = temporaryStudentService.findAllStuByClassId(new Page<ClassStuVo>(pageNum, pageSize), classId, ksh, name);
        return new WebResponse<PageInfo<ClassStuVo>>().success(PageInfo.toPageInfo(page));
    }



    /**
     * @Description: 根据班级id取消分班
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/cancelSepClaByClaId", method = RequestMethod.POST)
    public WebResponse<Integer> cancelSepClassByClassId(@RequestBody List<String> list){
        int result = temporaryStudentService.cancelSepClassByClassId(list);
        return new WebResponse<Integer>().success(result);
    }



    /**
     * @Description: 分班
     * @author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/sepClass", method = RequestMethod.POST)
    public WebResponse<Integer> sepClass(@RequestBody SepClassDto dto){
        int result = temporaryStudentService.sepClass(dto.getClassId(),dto.getList());
        return new WebResponse<Integer>().success(result);
    }



    /**
     * @Description: 根据xsid集合取消分班
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/cancelSepClaByXsids", method = RequestMethod.POST)
    public WebResponse<Integer> cancelSepClaByXsids( @RequestBody List<String> xsids){
        int result = temporaryStudentService.cancelSepClaByXsid(xsids);
        return new WebResponse<Integer>().success(result);
    }

}
